package org.jeecg.modules.demo.task;

import org.jeecg.modules.demo.crm.entity.CPaymentStatistics;
import org.jeecg.modules.demo.crm.entity.CProjectApplication;
import org.jeecg.modules.demo.crm.mapper.CInvoiceApplyMapper;
import org.jeecg.modules.demo.crm.mapper.CPaymentInvoiceMapper;
import org.jeecg.modules.demo.crm.mapper.CPaymentStatisticsMapper;
import org.jeecg.modules.demo.crm.mapper.CProjectApplicationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Component
public class LineMultiScheduledTask {

    @Autowired
    private CProjectApplicationMapper projectApplicationMapper;
    @Autowired
    private CPaymentInvoiceMapper paymentInvoiceMapper;
    @Autowired
    private CInvoiceApplyMapper invoiceApplyMapper;
    @Autowired
    private CPaymentStatisticsMapper paymentStatisticsMapper;

    // 每天凌晨 0 点 0 分 0 秒执行一次
    @Scheduled(cron = "0 0 0 * * ?")
    // 每10分钟执行一次
    //@Scheduled(cron = "0 */10 * * * ?")
    public void executeTask() {
        List<CProjectApplication> cProjectApplications = projectApplicationMapper.selectList(null);
        for (CProjectApplication cProjectApplication : cProjectApplications) {
            BigDecimal totalPayable =  paymentInvoiceMapper.totalPayable(cProjectApplication.getId());
            BigDecimal totalTeceivables =invoiceApplyMapper.totalTeceivables(cProjectApplication.getId());

            CPaymentStatistics cPaymentStatistics = new CPaymentStatistics();
            cPaymentStatistics.setAmount(totalPayable);
            cPaymentStatistics.setType("应收");
            cPaymentStatistics.setProjectId(cProjectApplication.getId());
            cPaymentStatistics.setStatisticsTime(new Date());
            paymentStatisticsMapper.insert(cPaymentStatistics);

            CPaymentStatistics cPaymentStatistics2 = new CPaymentStatistics();
            cPaymentStatistics2.setAmount(totalTeceivables);
            cPaymentStatistics2.setType("应付");
            cPaymentStatistics2.setProjectId(cProjectApplication.getId());
            cPaymentStatistics2.setStatisticsTime(new Date());
            paymentStatisticsMapper.insert(cPaymentStatistics2);

        }

        System.out.println("定时任务执行了！当前时间：" + new java.util.Date());
    }
}
